package com.trade.okxserver.manage;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Stack;

public class SecondKlineCache {

    private static Cache<String, Stack<List<Double>>> cache = Caffeine.newBuilder()
            .maximumSize(500)
            .build();

    public synchronized static Stack<List<Double>> get(String key) {
        Stack<List<Double>> dataList = cache.getIfPresent(key);
        if(CollectionUtils.isEmpty(dataList)){
            return new Stack<>();
        }
        return dataList;
    }

    public synchronized static void set(String key, List<List<Double>> data) {
        Stack<List<Double>> dataList = cache.getIfPresent(key);
        if (Objects.isNull(dataList)) {
            dataList = new Stack<>();
        }
        if(dataList.size() > 172800){
            int size = data.size();
            for (int i = 0; i < size; i++) {
                dataList.pop();
            }
            dataList.addAll(data);
        }
        dataList.addAll(data);
        cache.put(key, dataList);
    }


}
